package com.yan_jiu_sheng.daily.AC;

/**
 * https://leetcode.cn/problems/lexicographically-smallest-string-after-operations-with-constraint/description/?envType=daily-question&envId=2024-07-27
 * 贪心
 * 参考提示和评论通过
 *
 * @author yulongtian
 * @create 2024-07-27 10:39
 */
public class Test20240727 {
    public static void main(String[] args) {
        System.out.println(new Test20240727().getSmallestString("zbbz", 3));
    }

    public String getSmallestString(String s, int k) {
        char[] chs = s.toCharArray();
        for (int i = 0; i < chs.length; i++) {
            char c = chs[i];
            int dist1 = 'z' - c + 1, dist2 = c - 'a';
            if (dist1 > k && dist2 > k) {
                chs[i] = (char) (chs[i] - k);
                return new String(chs);
            }
            if (dist1 <= dist2 && dist1 <= k) {
                k -= dist1;
                chs[i] = 'a';
            } else {
                k -= dist2;
                chs[i] = 'a';
            }

        }

        return new String(chs);
    }

}
